package com.example.chatrecommendation.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.chatrecommendation.bean.dto.ExamResultDTO;
import com.example.chatrecommendation.bean.entity.StudentRecord;
import com.example.chatrecommendation.bean.entity.TDA;
import com.example.chatrecommendation.bean.vo.TDAVO;
import com.example.chatrecommendation.mapper.StudentRecordMapper;
import com.example.chatrecommendation.service.KnowledgeTagService;
import com.example.chatrecommendation.service.StudentRecordService;
import com.example.chatrecommendation.tool.BaseContext;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @author Jasmine-drs
 * @description 针对表【student_record(学生学习记录)】的数据库操作Service实现
 * @Date 2024-03-07 17:22:50
 */
@Service
public class StudentRecordServiceImpl extends ServiceImpl<StudentRecordMapper, StudentRecord> implements StudentRecordService {

	private final StudentRecordMapper studentRecordMapper;
	private final KnowledgeTagService knowledgeTagService;

	public StudentRecordServiceImpl(StudentRecordMapper studentRecordMapper, KnowledgeTagService knowledgeTagService) {
		this.studentRecordMapper = studentRecordMapper;
		this.knowledgeTagService = knowledgeTagService;
	}

	@Override
	public void saveExamResult(ExamResultDTO examResultDTO, Integer studentId) {
		List<StudentRecord> studentRecord = new ArrayList<>();
		for (int i = 0; i < examResultDTO.getQuestionId().size(); i++) {
			StudentRecord studentRecord1 = new StudentRecord();
			studentRecord1.setStudentId(studentId);
			studentRecord1.setType(3);
			studentRecord1.setValueId((Integer) examResultDTO.getQuestionId().toArray()[i]);
			studentRecord1.setIsTrue((Boolean) examResultDTO.getResult().toArray()[i] ? 1 : 0);
			studentRecord1.setTime((Double) examResultDTO.getAnswerTimes().toArray()[i]);
			studentRecord1.setTrueTime((Long) examResultDTO.getUseTrueTimes().toArray()[i]);
			studentRecord1.setDifficulty((Double) examResultDTO.getDifficulty().toArray()[i]);
			studentRecord1.setCreateTime(LocalDateTime.now());
			studentRecord1.setUpdateTime(LocalDateTime.now());
			studentRecord.add(studentRecord1);
		}
//        批量插入
		saveBatch(studentRecord);

		HashMap<Integer, String> map = new HashMap<>();
		for (int i = 0; i < examResultDTO.getQuestionId().size(); i++) {
			if (examResultDTO.getResult().get(i)){
//			map.put(examResultDTO.getQuestionId().get(i), examResultDTO.getQuestionTypes().get(i));
				map.put(examResultDTO.getQuestionId().get(i), String.valueOf(3));
			}
		}
		knowledgeTagService.getKnowledgeName(map, studentId);
	}

	@Override
	public void saveTdaResult(Double tda) {
		Integer stuId = BaseContext.getCurrentId();
		stuId = 234567;
		TDA tdaData = TDA.builder()
				.stuId(stuId)
				.tda(tda)
				.courseId(2)
				.updateTime(LocalDateTime.now())
				.build();

		studentRecordMapper.updateTDAById(tdaData);
//        studentRecordMapper.insertTDA(tdaData);
	}

	@Override
	public TDAVO getTda(Integer stuId) {
//		Integer stuId = BaseContext.getCurrentId();
//		stuId = 123456;

		TDAVO tdaInfo = studentRecordMapper.getTdaInfo(stuId);
		return tdaInfo;
	}
}




